package cn.hfatec.shms.system.authData.service;

import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.base.BaseDao;
import cn.hfatec.shms.exception.ShmsException;
import cn.hfatec.shms.system.authData.dao.AuthDataDao;
import cn.hfatec.shms.system.authData.model.AuthData;
import cn.hfatec.shms.system.authData.model.AuthDataParams;
import cn.hfatec.shms.system.authResource.dao.AuthResourceDao;
import cn.hfatec.shms.system.moduleAuth.dao.ModuleAuthDao;
import cn.hfatec.shms.system.moduleAuth.model.ModuleAuth;
import cn.hfatec.shms.system.resource.dao.ResourceDao;
import cn.hfatec.shms.system.roleAuth.dao.RoleAuthDao;
import com.google.common.base.Optional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("AuthDataService")
public class AuthDataService {

    public static final String BEAN_ID = "AuthDataService";

    //注入Dao层
    @Resource(name = "AuthDataDao")
    private AuthDataDao authDataDao;

    @Resource(name = "RoleAuthDao")
    private RoleAuthDao roleAuthDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    @Resource(name = "AuthResourceDao")
    private AuthResourceDao authResourceDao;

    @Resource(name = "ModuleAuthDao")
    private ModuleAuthDao moduleAuthDao;

    @Resource(name = "ResourceDao")
    private ResourceDao resourceDao;


    /**
     *
     * 查询数据
     *
     * @param authDataParams
     * @param page
     *
     */
    @Transactional
    public Table queryAuthDataTable(AuthDataParams authDataParams, Page page){
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", authDataParams);
        return baseDao.queryTableByPage("queryAuthData", params, page);
    }

    /**
     *
     * 查询权限列表
     *
     * @param authDataParams
     * @return
     */
    @Transactional
    public List queryAuthDataList(AuthDataParams authDataParams){
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", authDataParams);
        return baseDao.queryList("queryAuthData", params);
    }

    /**
     *
     * 插入数据
     * @param authData
     *
     */
    @Transactional
    public void saveAuthData(AuthData authData,String moduleId) {
        if(authData.getId() != null) {
            AuthData result = getAuthData(authData.getId());
            if(result != null) {
                authDataDao.update(authData);
            } else {
                authDataDao.insert(authData);
                ModuleAuth moduleAuth = new ModuleAuth();
                moduleAuth.setAuthId(authData.getAuthId());
                moduleAuth.setModuleId(moduleId);
                moduleAuthDao.insert(moduleAuth);
            }
        } else {
            authDataDao.insert(authData);
            ModuleAuth moduleAuth = new ModuleAuth();
            moduleAuth.setAuthId(authData.getAuthId());
            moduleAuth.setModuleId(moduleId);
            moduleAuthDao.insert(moduleAuth);
        }
    }

    /**
     *
     * 删除数据
     * @param id
     *
     */
    @Transactional
    public void deleteAuthData(Integer id, String moduleId) throws ShmsException {
        AuthData authData = getAuthData(id);
        roleAuthDao.deleteRoleAuthByAuthId(authData.getAuthId());
        resourceDao.deleteByAuthId(authData.getAuthId());
        authResourceDao.deleteAuthResourceByAuthId(authData.getAuthId());
        moduleAuthDao.deleteModuleAuthForAuthId(moduleId, authData.getAuthId());
        authDataDao.delete(id);
    }

    /**
     *
     * 删除数据
     * @param authDataList
     *
     */
    @Transactional
    public void deleteAuthDataBatch(List<String> authDataList, String moduleId) throws ShmsException {
        for(String id : authDataList) {
            deleteAuthData(Integer.valueOf(id), moduleId);
        }
    }

    /**
     *
     * 根据ID获取数据
     * @param id
     *
     */
    @Transactional
    public AuthData getAuthData(Integer id) {

        return authDataDao.getAuthDataById(id);
    }

    /**
     *
     * 检查权限ID是否重复
     *
     * @param authData
     * @return
     */
    @Transactional
    public Optional checkAuthIdRepeat(AuthData authData) {
        if(authDataDao.getAuthDataByAuthId(authData.getAuthId()) != null) {
            return Optional.of(new JsonResponse<String>(JsonResponse.RETURN_FAILURE, "权限ID重复，不能新增"));
        }
        return Optional.absent();
    }

}